<template>
	<wd-popup v-model="drawerVisible" custom-style="width:95%; height:40%" lock-scroll>

		<wd-card type="rectangle">
			<template #title>

				<span>【{{ parentTask.name }}】审批人：{{ parentTask?.assigneeUser?.nickname }}</span>
				<span class="ml-12 mt-16">
					<wd-button v-if="isSignDeleteButtonVisible(parentTask)" size="small" type="warning"
						@click="handleSignDelete(parentTask)" icon="minus-circle">减签
					</wd-button>
				</span>

			</template>
			<view class="">
				<wd-table v-if="parentTask.children" :data="parentTask.children" :border="false" :height="300">
					<wd-table-col prop="assigneeUser.nickname" label="审批人" sortable>
						<template #value="{ row }">
							{{ row.assigneeUser?.nickname || row.ownerUser?.nickname }}
						</template>
					</wd-table-col>
					<wd-table-col prop="assigneeUser.deptName" label="所在部门" sortable>
						<template #value="{ row }">
							{{ row.assigneeUser?.deptName || row.ownerUser?.deptName }}
						</template>
					</wd-table-col>

					<wd-table-col prop="grade" label="审批状态" align="center">
						<template #value="{ row }">
							<wd-tag custom-class="space" type="primary">
								{{ getDict(DICT_TYPE.BPM_TASK_STATUS, row.status) }}
							</wd-tag>
						</template>
					</wd-table-col>
					<wd-table-col prop="createTime" label="提交时间" sortable :width="150">
						<template #value="{ row }">
						
								{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss')}}
						
						</template>
					</wd-table-col>
					<wd-table-col prop="endTime" label="结束时间" sortable :width="150">
						<template #value="{ row }">
							{{ dayjs(row.endTime).format('YYYY-MM-DD HH:mm:ss')}}
						</template>
					</wd-table-col>
					<wd-table-col prop="grade" label="操作" width="200" fixed align="center">
						<template #value="{ row }">
							<wd-button v-if="isSignDeleteButtonVisible(row)" type="warning"
								@click="handleSignDelete(row)" icon="minus-circle" size="small">
								减签
							</wd-button>
						</template>
					</wd-table-col>
				</wd-table>


			</view>
		</wd-card>
		<!-- 减签 -->
		<TaskSignDeleteForm ref="taskSignDeleteFormRef" @success="handleSignDeleteSuccess" />
	</wd-popup>
</template>
<script lang="ts" setup>
	import dayjs from 'dayjs'
	import { ref, reactive } from 'vue'
	import { isEmpty } from '@/utils/is'
	import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
	import TaskSignDeleteForm from './TaskSignDeleteForm.vue'
	import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
	import { useMessage, useToast } from 'wot-design-uni'

	defineOptions({ name: 'TaskSignList' })

	const toast = useToast()  // 消息弹窗 // 消息弹窗
	const drawerVisible = ref(false) // 抽屉的是否展示
	const parentTask = ref({} as any)

	const dataList = reactive([
		{
			name: '赵云',
			school: '武汉市阳逻妇幼保健学院',
			major: '计算机科学与技术专业'
		},
		{
			name: '孔明',
			school: '武汉市阳逻卧龙学院',
			major: '计算机科学与技术专业'
		},
		{
			name: '刘备',
			school: '武汉市阳逻编织学院',
			major: '计算机科学与技术专业'
		}
	])
	const getDict = (type : any, value : any) => {
		const dicts = getIntDictOptions(type)
		const dict = dicts.find(item => item.value === value)
		return dict ? dict.label : '待审批'
	}
	/** 打开弹窗 */
	const open = async (task : any) => {
		if (isEmpty(task.children)) {
			toast.warning('该任务没有子任务')
			return
		}
		parentTask.value = task
		console.log('parentTask.value', parentTask.value);
		// 展开抽屉
		drawerVisible.value = true
	}
	defineExpose({ open }) // 提供 openModal 方法，用于打开弹窗

	/** 发起减签 */
	const taskSignDeleteFormRef = ref()
	const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
	const handleSignDelete = (item : any) => {
		taskSignDeleteFormRef.value.open(item.id)
	}
	const handleSignDeleteSuccess = () => {
		emit('success')
		// 关闭抽屉
		drawerVisible.value = false
	}

	/** 是否显示减签按钮 */
	const isSignDeleteButtonVisible = (task : any) => {
		return task && task.children && !isEmpty(task.children)
	}
</script>